package com.starblink.android.common.utils

import android.app.Activity
import android.util.Log
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import com.apollographql.apollo3.api.Optional
import com.starblink.android.basic.ext.click
import com.starblink.android.basic.ext.toBean
import com.starblink.android.basic.ext.toJson
import com.starblink.android.basic.extension.navigationTo
import com.starblink.android.basic.modules.mine.MineManager
import com.starblink.basic.apollo.ext.optional
import com.starblink.android.basic.util.ImageUtils
import com.starblink.android.basic.widget.flowlayout.FlowLayout
import com.starblink.android.basic.widget.flowlayout.TagAdapter
import com.starblink.android.basic.widget.flowlayout.TagFlowLayout
import com.starblink.android.common.R
import com.starblink.android.common.utils.post.ProductCollectionStatus
import com.starblink.basic.route.RoutePage
import com.starblink.basic.util.CommUtils
import com.starblink.rocketreserver.fragment.PostD
import com.starblink.rocketreserver.type.ProductCollectionStatusReq
import java.net.URLEncoder

object PostDetailHelp {

    /**
     * 同步点赞状态
     */
    fun changeLikeState(
        liked: Boolean = false,
        likeNum: Int,
        ivLike: ImageView,
        tvLike: TextView
    ) {
        if (liked) {
            ivLike.setImageResource(R.mipmap.detail_like)
        } else {
            ivLike.setImageResource(
                R.mipmap.detail_unlike
            )
        }
        tvLike.text = "$likeNum"
    }

    /**
     * 展示运营话题
     */
    fun showTopicView(
        activity: Activity,
        operatorHashtagList: List<PostD.OperatorHashtagList?>?,
        flTopic: TagFlowLayout
    ) {
        if (operatorHashtagList?.isNotEmpty() == true) {
            flTopic.visibility = View.VISIBLE
            flTopic.adapter =
                object : TagAdapter<PostD.OperatorHashtagList?>(operatorHashtagList) {
                    override fun getView(
                        parent: FlowLayout?,
                        position: Int,
                        s: PostD.OperatorHashtagList?
                    ): View {
                        val tv: TextView = View.inflate(
                            activity,
                            R.layout.item_topic,
                            null
                        ) as TextView
                        if (s != null) {
                            tv.text = "#${s.name}"
                        }
                        return tv
                    }
                }
            flTopic.setOnTagClickListener { view, position, parent ->
                activity.navigationTo(RoutePage.Post.PAGE_TOPIC) {
                    withString(RoutePage.Post.TOPIC_ID, operatorHashtagList[position]?.id)
                }
                true
            }
        } else {
            flTopic.visibility = View.GONE
        }
    }

    /**
     * 展示用户信息
     */
    fun showPostUser(
        activity: Activity,
        posterInfo: PostD.PosterInfo,
        ivUser: ImageView,
        tvName: TextView,
        deleteColor: Int = com.starblink.basic.style.R.color.sk_gray400,
        nameColor: Int = com.starblink.basic.style.R.color.sk_gray800,
        needAt: Boolean = false
    ) {
        if (needAt) {
            tvName.text = "@ ${posterInfo.poster.username}"
        } else {
            tvName.text = posterInfo.poster.username ?: ""
        }

        ImageUtils.loadImage(
            activity,
            posterInfo.poster.headImage,
            ivUser,
            com.starblink.basic.style.R.mipmap.mine_default_avatar,
            com.starblink.basic.style.R.mipmap.mine_default_avatar
        )
        if (posterInfo.poster.deleted == true) {
            tvName.text = "Account Deleted"
            tvName.setTextColor(CommUtils.getColor(deleteColor))
        } else {
            tvName.setTextColor(CommUtils.getColor(nameColor))
        }

        ivUser.click {//跳转个人中心
            MineManager.navigationToOtherUserPersonHome(
                activity,
                posterInfo.poster.id,
                posterInfo.poster.deleted,
                posterInfo.poster.username
            )
        }
        tvName.click {//跳转个人中心
            MineManager.navigationToOtherUserPersonHome(
                activity,
                posterInfo.poster.id,
                posterInfo.poster.deleted,
                posterInfo.poster.username
            )
        }
    }

    /**
     * 获取分享挂载商品参数
     */
    fun getShareProductListString(products: List<PostD.ProductList1>?): String {
        var productList = mutableListOf<Map<String, Any>>()
        Log.e("getShareProduct", "1====$products")
        for (i in products?.indices!!) {
            val it = products[i]
//            val outOfferId: Double = it.productF.outOfferId as Double
            var outId = it.productF.outOfferId.toString()
            if (outId == "0.0") {
                outId = "0"
            }
            if (i < 5) {
                var map = mapOf(
                    "productId" to "${it.productF.id}",
                    "productType" to it.productF.type,
                    "outOfferId" to outId
                )
                productList.add(map)
            }
        }
        Log.e("getShareProduct", "2====$productList")
//        products?.forEach { it ->
//            var map = mapOf(
//                "productId" to "${it.productF.id}",
//                "productType" to "${it.productF.type}",
//                "outOfferId" to "${it.productF.outOfferId}"
//            )
//            productList.add(map)
//        }
        return URLEncoder.encode(productList.toJson(), "utf-8")
    }

    /**
     * 分享app解析挂载商品参数
     */
    fun getShareProductList(products: String): List<ProductCollectionStatusReq>? {
        return try {
            val list: List<ProductCollectionStatus> =
                products.toBean<List<ProductCollectionStatus>>()

            val list2: List<ProductCollectionStatusReq> = list.mapNotNull {
                ProductCollectionStatusReq(
                    it.productId,
                    it.productType,
                    if (it.outOfferId.isNotEmpty()) it.outOfferId.optional() else Optional.absent()
                )
            }
            list2

//            products.toBean<List<ProductCollectionStatusReq>>()
        } catch (e: Exception) {
            Log.e("ProductRecommendList", "----==$products==$e")
            null
        }
    }
}